package com.ruoyi.web.controller.laiqianba.channel;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.channel.enums.ChannelTypeEnum;
import com.ruoyi.channel.enums.PageTypeEnum;
import com.ruoyi.channel.service.IChannelBaseInfoService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.laiqianba.enums.*;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.db.domain.ChannelBaseInfo;
import com.ruoyi.laiqianba.service.IApplicationInfoService;
import com.ruoyi.laiqianba.service.IMemberSourceDictInfoService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.service.IChannelService;
import com.ruoyi.web.wrapper.ChannelBaseInfoWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 渠道基本信息Controller
 * 
 * @author ruoyi
 * @date 2023-09-01
 */
@RestController
@RequestMapping("/channel/baseInfo")
public class  ChannelBaseInfoController extends BaseController
{
    @Autowired
    private IChannelBaseInfoService channelBaseInfoService;
    @Autowired
    private IApplicationInfoService applicationInfoService;
    @Autowired
    ISysUserService sysUserService;
    @Autowired
    IChannelService channelService;
    @Autowired
    IMemberSourceDictInfoService memberSourceDictInfoService;



    /**
     * 修改渠道基本信息
     * /channel/baseInfo/extData
     */
    @GetMapping("/extData")
    public AjaxResult extData()
    {

        JSONObject returnData = new JSONObject();
        returnData.put("pageTypeMapList", PageTypeEnum.getPageTypeMapList());
        returnData.put("chargeTypeList", ChargeTypeEnum.getChannelMapList());
        returnData.put("channelTypeList", ChannelTypeEnum.getChannelTypeMapList());
        returnData.put("channelNameList", channelBaseInfoService.getChannelNameList());
        returnData.put("channelNameOpen", channelBaseInfoService.getChannelNameListOpen());
        returnData.put("appInfoList", applicationInfoService.getAppInfoMapList());
        returnData.put("flowRateTypeList", FlowRateTypeEnum.FlowRateTypeMapList());
        returnData.put("checkTypeList", CheckStatusEnum.getMapList());


        returnData.put("zhimaMapList", BaseTypeInfoEnum.ZhimaScore.getZhimaMapList());
        returnData.put("jobMapList",BaseTypeInfoEnum.Gongzuo.getDataMap());
        returnData.put("loanAmountMapList",BaseTypeInfoEnum.LoanAmount.getDataMap());
        returnData.put("loanTimeMapList",BaseTypeInfoEnum.LoanTime.getDataMap());
        returnData.put("xueliMapList",BaseTypeInfoEnum.XueLi.getDataMap());
        returnData.put("yuqiMapList",BaseTypeInfoEnum.Yuqi.getDataMap());
        returnData.put("yongtuMapList",BaseTypeInfoEnum.Yongtu.getDataMap());
        returnData.put("shouruMapList",BaseTypeInfoEnum.Shouru.getDataMap());
        returnData.put("orFilterMapList",memberSourceDictInfoService.getOrFilterAll());
        returnData.put("channelCodeAppList",channelBaseInfoService.getChannelNameList4());

        // 商务列表信息
        // 单独的个人还是，整体的商务列表
        returnData.put("ownerInfoList", sysUserService.getUserInfoByPostId(PostInfoEnum.CHANNEL_MANAGER.getPostId()));

        return AjaxResult.success(returnData);
    }

    /**
     * 查询渠道基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('channel:baseInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(ChannelBaseInfo channelBaseInfo)
    {

//       if (!SecurityUtils.loginUserIsAdmin() ){
//           channelBaseInfo.setOwnerId(SecurityUtils.getUserId());
//       }

        startPage();
        List<Map<String, Object>> list = channelBaseInfoService.selectChannelBaseInfoMapList(channelBaseInfo);
        list = new ChannelBaseInfoWrapper(list).wrap();
        return getDataTable(list);
    }


    /**
     * 获取渠道基本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('channel:baseInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(channelBaseInfoService.selectChannelBaseInfoById(id));
    }

    /**
     * 新增渠道基本信息
     */
    @PreAuthorize("@ss.hasPermi('channel:baseInfo:add')")
    @Log(title = "渠道基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChannelBaseInfo channelBaseInfo)
    {
        return channelService.insertChannelBaseInfo(channelBaseInfo);
    }

    /**
     * 修改渠道基本信息
     */
    @PreAuthorize("@ss.hasPermi('channel:baseInfo:edit')")
    @Log(title = "渠道基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChannelBaseInfo channelBaseInfo)
    {
        SysUser sysUser = sysUserService.selectUserById(channelBaseInfo.getOwnerId());
        channelBaseInfo.setOwnerName(sysUser.getNickName());

        return toAjax(channelBaseInfoService.updateChannelBaseInfo(channelBaseInfo));
    }

    /**
     * 删除渠道基本信息
     */
    @PreAuthorize("@ss.hasPermi('channel:baseInfo:remove')")
    @Log(title = "渠道基本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(channelBaseInfoService.deleteChannelBaseInfoById(id));
    }

    /**
     * 修改渠道状态
     */
    @PreAuthorize("@ss.hasPermi('channel:baseInfo:status')")
    @Log(title = "渠道状态修改", businessType = BusinessType.UPDATE)
    @PostMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody ChannelBaseInfo channelBaseInfo) {
        return toAjax(channelBaseInfoService.updateSmsStatus(channelBaseInfo));
    }


    /**
     * 修改短信状态
     */
    @PreAuthorize("@ss.hasPermi('channel:baseInfo:smsStaus')")
    @Log(title = "渠道短信状态修改", businessType = BusinessType.UPDATE)
    @PostMapping("/updateSmsStatus")
    public AjaxResult updateSmsStatus(@RequestBody ChannelBaseInfo channelBaseInfo) {
        return toAjax(channelBaseInfoService.updateSmsStatus(channelBaseInfo));
    }

    @PutMapping("/updateChannelBaseInfo")
    public AjaxResult updateChannelBaseInfo(@RequestBody ChannelBaseInfo channelBaseInfo)
    {
        String appCodeStr = channelBaseInfo.getAppCode();
        List<String> appCodes = Arrays.stream(appCodeStr.split(","))
                .map(String::trim)
                .filter(s -> !s.isEmpty())
                .collect(Collectors.toList());
        return toAjax(channelBaseInfoService.updateChannelBaseInfoByTask(appCodes,channelBaseInfo));
    }

    @PutMapping("/updateChannelBaseInfo1")
    public AjaxResult updateChannelBaseInfo1(@RequestBody ChannelBaseInfo channelBaseInfo)
    {
        String appCodeStr = channelBaseInfo.getAppCode();
        List<String> appCodes = Arrays.stream(appCodeStr.split(","))
                .map(String::trim)
                .filter(s -> !s.isEmpty())
                .collect(Collectors.toList());
        return toAjax(channelBaseInfoService.updateChannelBaseInfoByTask1(appCodes,channelBaseInfo));
    }
}
